package com.yy.show.action;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.log.Logger;
import com.yy.base.log.LoggerFactory;
import com.yy.base.page.Message;
import com.yy.module.entity.ModuleInfo;
import com.yy.show.service.ShowService;
import com.yy.show.util.ModuleDeleteHandle;
import com.yy.show.util.ModuleQueryHandle;
import com.yy.show.util.ModuleSaveHandle;
import com.yy.show.util.ModuleTreeHandle;

@Controller
@RequestMapping(value = "/showAction")
@SuppressWarnings("unchecked")
public class ShowAction extends BaseCtrl {
	private static final long serialVersionUID = 1L;
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ShowService service;
	
	/**
	 * 表格分页查询
	 */
	@ResponseBody
	@RequestMapping(value="/getQueryList.html")
	public Message getQueryList(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			ModuleInfo module = service.getModuleByCode(code);
			ModuleQueryHandle handle = new ModuleQueryHandle(module, request);
			if (handle.hasError()) {
				message.setErrorMessage(handle.getError());
			} else {
				if (module.getPagination() != null && module.getPagination() == 1) {
					message.setPageList(service.splitQueryList(handle));
				} else {
					message.setPageList(service.getQueryList(handle));
				}
			}
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询数据异常！");
		}
		return message;
	}
	/**
	 * 获取页面数据
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleData.html")
	public Message getModuleData(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			ModuleInfo module = service.getModuleByCode(code);
			ModuleQueryHandle handle = new ModuleQueryHandle(module, request);
			if (handle.hasError("only")) {
				message.setErrorMessage(handle.getError());
			} else {
				message.setData(service.getModuleData(handle));
			}
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询数据异常！");
		}
		return message;
	}
	/**
	 * 初始化页数据
	 */
	@ResponseBody
	@RequestMapping(value="/initPage.html")
	public Message initPage(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			message.setData(service.initPage(code));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("得到页面初始化信息异常！");
		}
		return message;
	}
	/**
	 * 查询模块展示信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleFieldList.html")
	public Message getModuleFieldList(HttpServletRequest request) {
		Message message = new Message();
		try {
			message.setData("list", service.getModuleFieldList(request.getParameter("moduleId")));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询异常！");
		}
		return message;
	}
	/**
	 * 初始化树形结构数据
	 */
	@ResponseBody
	@RequestMapping(value="/getTreeData.html")
	public Message getTreeData(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			ModuleInfo module = service.getModuleByCode(code);
			ModuleTreeHandle handle = new ModuleTreeHandle(module, request);
			if (handle.hasError()) {
				message.setErrorMessage(handle.getError());
			} else {
				message.setData(service.getTreeData(handle));
			}
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("初始化树形结构数据异常！");
		}
		return message;
	}
	/**
	 * 查询模块查询条件信息
	 */
	@ResponseBody
	@RequestMapping(value="/getModuleQueryList.html")
	public Message getModuleQueryList(HttpServletRequest request) {
		Message message = new Message();
		try {
			message.setData("list", service.getModuleQueryList(request.getParameter("moduleId")));
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("查询异常！");
		}
		return message;
	}
	/**
	 * 保存数据
	 */
	@ResponseBody
	@RequestMapping(value="/saveData.html")
	public Message saveData(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			String type = request.getParameter("type");
			ModuleInfo module = service.getModuleByCode(code);
			ModuleSaveHandle handle = new ModuleSaveHandle(module, request);
			if ("add".equals(type)) {
				if (handle.hasInsertError()) {
					message.setErrorMessage(handle.getError());
					return message;
				}
				
				//唯一性验证
				if (handle.getOnlyField() != null) {
					List<Map> list = handle.getOnlyField();
					for (Map m : list) {
						Long sqlCount = service.getDao().getSqlCount((String)m.get("sql"), m.get("value"));
						if (sqlCount > 0) {
							message.setErrorMessage(m.get("name") + "[" + m.get("value") + "]已存在！");
							return message;
						}
					}
				}
			} else if (handle.hasUpdateError()) {
				message.setErrorMessage(handle.getError());
				return message;
			}
			service.saveData(handle);
			message.setOkMessage("保存成功！");
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("保存异常！");
		}
		return message;
	}
	/**
	 * 删除数据
	 */
	@ResponseBody
	@RequestMapping(value="/deleteData.html")
	public Message deleteData(HttpServletRequest request) {
		Message message = new Message();
		try {
			String code = request.getParameter("code");
			ModuleInfo module = service.getModuleByCode(code);
			ModuleDeleteHandle handle = new ModuleDeleteHandle(module, request);
			if (handle.hasDeleteError()) {
				message.setErrorMessage(handle.getError());
				return message;
			}
			service.deleteData(handle);
			message.setOkMessage("删除成功！");
		} catch (Exception e) {
			log.error(e);
			message.setErrorMessage("删除异常！");
		}
		return message;
	}
}